package net.adetech.cjg.innerservice.utils;

import com.alibaba.fastjson.JSONObject;
import net.adetech.framework.entity.ServiceRequest;
import net.adetech.framework.entity.ServiceResponse;
import net.adetech.framework.utils.SerializeUtil;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;


@Service
public class InnerServiceUtils {

    @Value("${tunnel.url}")
    public String tunnelUrl;


    @Value("${outer_service.url}")
    public String outerServiceUrl;

    public ServiceResponse call(ServiceRequest request)
    {

        String serviceName = request.getServiceName();
        Map<String,Object> params = request.getParams();
        if(params==null)
        {
            params = new HashMap<String,Object>();
        }
        params.put("serviceName",serviceName);
        params.put("url",outerServiceUrl);
        request.setParams(params);
        request.setServiceName("cjg.call");

        String requestString = SerializeUtil.objectToString(request);
        try {

            StringEntity entity = new StringEntity("{data:'"+requestString+"'}","UTF-8");
            entity.setContentType("application/json");

            HttpClient client = HttpClients.createDefault();
            HttpPost post = new HttpPost(tunnelUrl);
            post.setEntity(entity);

            HttpResponse httpResponse = client.execute(post);
            String responseString = EntityUtils.toString(httpResponse.getEntity());


            JSONObject jsonObject = JSONObject.parseObject(responseString);
            String resultString = jsonObject.getString("result");
            ServiceResponse serviceResponse = (ServiceResponse) SerializeUtil.stringToObject(resultString);
            return serviceResponse;
        }catch (Exception e)
        {
            e.printStackTrace();
        }
        return  null;
    }


    public ServiceResponse call(String serviceName,ServiceRequest request)
    {
        request.setServiceName(serviceName);
        return call(request);
    }

    public ServiceResponse callService(String serviceName,Object... parameters)
    {
        ServiceRequest request = new ServiceRequest();
        Map<String,Object> params = new HashMap<String, Object>();
        request.setServiceName(serviceName);
        for (int i =0;i<parameters.length;i+=2)
        {
            String key = (String)parameters[i];
            Object value = parameters[i+1];
            params.put(key,value);
        }
        request.setParams(params);
        return  this.call(request);
    }
    public static Map<String,Object> buildResult(Object... params)
    {
        Map<String,Object> resultMap = new HashMap<String, Object>();
        for (int i=0;i<params.length;i+=2)
        {
            resultMap.put((String)params[i],params[i+1]);

        }
        return resultMap;
    }


}
